package com.dl.live.controller;

import com.dl.live.entity.WebResDTO;
import com.dl.user.dto.UserDTO;
import com.dl.user.inter.IUserRPCService;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/user")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);
    @DubboReference//这个注解可以标识这个变量为DUbbo的引用，可以当作一个bean使用
    private IUserRPCService userRPCService;

    @RequestMapping("/getUserByID")
    public WebResDTO getUserByID(String userId) {

        logger.info("查询用户信息，userId:" + userId);

        //调用RPC服务
        UserDTO userByID = userRPCService.getUserByID(2L);
        if (userByID == null){
            return new WebResDTO(WebResDTO.ERROR_CODE,"用户不存在");
        }else{
            return new WebResDTO(WebResDTO.SUCCESS_CODE,userByID);
        }
    }


    @PostMapping("/sendSMS")
    public WebResDTO sendSMS(String mobile){
        if (!StringUtils.hasText(mobile)){
            return new WebResDTO(WebResDTO.ERROR_CODE,"手机号不能为空");
        }
        //调用RPC服务,发送短信
        if(userRPCService.sendLoginCode(mobile)){
            return new WebResDTO(WebResDTO.SUCCESS_CODE,"发送成功");
        }else {
            return new WebResDTO(WebResDTO.ERROR_CODE,"发送失败,请重试");
        }
    }
}
